

Type EditorWindow

	Field mainwin:TGadget
	Field canvas:TGadget
	Field canvastimer:TTimer
	Field children:TList = New TList
	Field gadget_children:TList = New TList
	Field parent:EditorWindow
	Method New()
		
		canvastimer = CreateTimer(30)
		
		
	End Method
	
	
	
	Method Update(EID:Int)
	
		Select EID
		
			Case EVENT_GADGETPAINT
				UpdateCanvas()
				
			Case EVENT_TIMERTICK
				Select EventSource()
				
					Case canvastimer
						UpdateCanvas()
							
				
				End Select

		
			
		End Select
		UpdateUI(EID)
		For Local e:EditorWindow = EachIn children
			e.UpdateUI(EID)
		Next
	End Method
	
	Method UpdateUI(EID:Int)
	
	End Method
	
	Method UpdateCanvas()
		If canvas = Null Then Return
		SetGraphics CanvasGraphics(canvas)
		Cls
		Flip
	End Method
	

End Type

Type MainEditor Extends EditorWindow

	Field filemenu:TGadget
	Field editmenu:TGadget
	Field helpmenu:TGadget
	Field RPGFile:String
	
	Field RPGUnlumped:Byte = False
	Const MENU_NEW=101
	Const MENU_OPEN=102
	Const MENU_SAVE=103
	Const MENU_IMPORT=901
	
	Const MENU_CLOSE=104
	Const MENU_EXIT=105
	
	Const MENU_GFX=106
	Const MENU_COPY=107
	Const MENU_WALKABOUT=108
	Const MENU_HERO=109
	Const MENU_ABOUT=209
	Const MENU_SM_ENEMY=210
	Const MENU_MD_ENEMY=211
	Const MENU_LG_ENEMY=212
	Const MENU_ATTACKS=213
	Const MENU_WEAPONS=214
	Const MENU_BOXES=215
	Const MENU_PORTRAIT=216
	Const MENU_TILESET=217
	
	Const MENU_EXPORT_XML = 1000
	Const MENU_EXPORT_HTML5 = 1001
	Const MENU_EXPORT_FLASH = 1002

	Method UpdateUI(EID:Int)
		
		Select EID
			Case EVENT_WINDOWCLOSE
				
				Select EventSource()
				
					Case mainwin
						FreeGadget(mainwin)
						End
						
						
				
				End Select
				
			Case EVENT_MENUACTION
				Select EventData()
					Case MENU_EXIT
						End
					
					Case MENU_TILESET
						Local mapsel:MaptilesSelector = New MaptilesSelector
						mapsel.Setup(Self)
						
					Case MENU_WALKABOUT	
						Local walkabout_frame_captions:String[] = ["Up A","Up B","Right A","Right B","Down A","Down B","Left A","Left B"]
						Local spritesel:SpriteSelector = New SpriteSelector
						spritesel.Setup(SpriteType.sprTypeWalkabout,walkabout_frame_captions,Self)
						children.Addlast(spritesel)

					Case MENU_HERO	
						Local walkabout_frame_captions:String[] = ["Standing","Stepping","Attack A","Attack B","Cast/Use","Hurt","Weak","Dead"]
						Local spritesel:SpriteSelector = New SpriteSelector
						spritesel.Setup(SpriteType.sprTypeHero,walkabout_frame_captions,Self)
						children.Addlast(spritesel)
						
					Case MENU_SM_ENEMY
						Local walkabout_frame_captions:String[] = ["Enemy (facing right)"]
						Local spritesel:SpriteSelector = New SpriteSelector
						spritesel.Setup(SpriteType.sprTypeSmallEnemy,walkabout_frame_captions,Self)
						children.Addlast(spritesel)

					Case MENU_MD_ENEMY
						Local walkabout_frame_captions:String[] = ["Enemy (facing right)"]
						Local spritesel:SpriteSelector = New SpriteSelector
						spritesel.Setup(SpriteType.sprTypeMediumEnemy,walkabout_frame_captions,Self)
						children.Addlast(spritesel)
					
					Case MENU_LG_ENEMY
						Local walkabout_frame_captions:String[] = ["Enemy (facing right)"]
						Local spritesel:SpriteSelector = New SpriteSelector
						spritesel.Setup(SpriteType.sprTypeLargeEnemy,walkabout_frame_captions,Self)
						children.Addlast(spritesel)

					Case MENU_ATTACKS
						Local walkabout_frame_captions:String[] = ["First Frame","Middle Frame","Last Frame"]
						Local spritesel:SpriteSelector = New SpriteSelector
						spritesel.Setup(SpriteType.sprTypeAttack,walkabout_frame_captions,Self)
						children.Addlast(spritesel)

					Case MENU_WEAPONS
						Local walkabout_frame_captions:String[] = ["Frame 1","Frame 2"]
						Local spritesel:SpriteSelector = New SpriteSelector
						spritesel.Setup(SpriteType.sprTypeWeapon,walkabout_frame_captions,Self)
						children.Addlast(spritesel)


					Case MENU_BOXES
						Local walkabout_frame_captions:String[] = ["Top Left Corner","Top Edge Left","Top Edge","Top Edge Right","Top Right Corner","Left Edge Top","Right Edge Top","Left Edge","Right Edge","Left Edge Bottom","Right Edge Bottom","Bottom Left Corner","Bottom Edge Left","Bottom Edge","Bottom Edge Right","Bottom Right Corner"]
						Local spritesel:SpriteSelector = New SpriteSelector
						spritesel.Setup(SpriteType.sprTypeBoxBorder,walkabout_frame_captions,Self)
						children.Addlast(spritesel)
					
					Case MENU_PORTRAIT
						Local walkabout_frame_captions:String[] = ["Character Portrait"]
						Local spritesel:SpriteSelector = New SpriteSelector
						spritesel.Setup(SpriteType.sprTypePortrait,walkabout_frame_captions,Self)
						children.Addlast(spritesel)

						
					Case MENU_ABOUT
						Notify "OHRRPGCE~n(C)2013 & Vigilsoft"
						
					Case MENU_IMPORT
						'filter$="Image Files:png,jpg,bmp;Text Files:txt;All Files:*"
						RPGFile=RequestFile( "Select RPG to open","RPG Files:rpg" )
						

						If RPGFile <> "" Then
							RPGLump = New FileLump
							RPGLump.filename = RPGFile
							RPGLump.unlump(mainwin)
							RPGUnlumped = True
							Game = StripExt(StripDir(RPGLump.filename))
							GamePath = Replace(RPGLump.filename,StripDir(RPGLump.filename),"")
							SetStatusText( mainwin,"Loading Master Palette" )
							PollEvent()
							loadpalette(0)
							'start dumping all data here
							RPGLump.LoadGeneralData()
							'get all graphics
							SetStatusText( mainwin,"Loading Walkabout:"+Gen[genMaxNPCPic] )
							PollEvent()
							DebugLog "Loading Walkabout:"+Gen[genMaxNPCPic] 
							WalkaboutState= New SpriteState
							WalkaboutState.CreateState(20,20,Gen[genMaxNPCPic],8,5,10,4)
							SetStatusText( mainwin,"Loading Heros"+Gen[genMaxHeroPic] )
							PollEvent()
							HeroState = New SpriteState
							HeroState.CreateState(32,40,Gen[genMaxHeroPic],8,16,8,0)
							SetStatusText( mainwin,"Loading Small Enemy"+Gen[genMaxEnemy1Pic] )
							PollEvent()	
							SmallEnemyState = New SpriteState
							SmallEnemyState.CreateState(34,34,Gen[genMaxEnemy1Pic],1,2,5,1)
							SetStatusText( mainwin,"Loading Medium Enemy" )
							PollEvent()
							MediumEnemyState = New SpriteState
							MediumEnemyState.CreateState(50,50,Gen[genMaxEnemy2Pic],1,4,4,2)
							SetStatusText( mainwin,"Loading Large Enemy" )
							PollEvent()
							LargeEnemyState = New SpriteState
							LargeEnemyState.CreateState(80,80,Gen[genMaxEnemy3Pic],1,10,3,3)
							SetStatusText( mainwin,"Loading Attacks" )
							PollEvent()							
							AttackState = New SpriteState
							AttackState.CreateState(50,50,Gen[genMaxAttackPic],3,12,4,6)
							SetStatusText( mainwin,"Loading Weapons" )
							PollEvent()								
							WeaponState = New SpriteState
							WeaponState.CreateState(24,24,Gen[genMaxWeaponPic],2,2,6,5)
							SetStatusText( mainwin,"Loading Box Borders" )
							PollEvent()								
							BoxState = New SpriteState
							BoxState.CreateState(16,16,Gen[genMaxBoxBorder],16,7,10,7)
							SetStatusText( mainwin,"Loading Portraits" )
							PollEvent()													
							PortraitState = New SpriteState
							PortraitState.CreateState(50,50,Gen[genMaxPortrait],1,4,4,8)
							SetStatusText( mainwin,"Loading Music:" )
							PollEvent()
							DebugLog "Loading Music:" 
							MusicState = New SongState
							MusicState.CreateState()
							SetStatusText( mainwin,"Loading Sounds:" )
							PollEvent()							
							DebugLog "Loading Sounds"						
							SFXState = New SoundState
							SFXState.CreateState()
							SetStatusText( mainwin,"Loading Tilesets:" )
							PollEvent()							
							DebugLog "Loading Tilesets"							
							TileSState = New TilsetState
							TileSState.Loadsets()
							SetStatusText( mainwin,"Loading Backdrops:" )
							PollEvent()							
							DebugLog "Loading Backdrops"							
							
							BackdropSet	 = New BackdropState
							BackdropSet.Loadsets()
												
							'do one map for testing
							Local i:Int = 0
							Repeat
								Local m:MapeditState = New MapeditState
								Local havemap:Int = m.Loadmap(i)
								If havemap <> -1 Then
									ListAddLast(Maps,m)
									SetStatusText( mainwin,"Loading Map: "+m.mapname )
									PollEvent()										
								Else
									Exit
								EndIf
								i = i+1
							Forever

						EndIf
						SetStatusText( mainwin,"Import Complete!" )
						PollEvent()	
						

						Case MENU_EXPORT_XML
							Local exportpath:String=RequestDir("Export XML to Folder",CurrentDir())
							Local myfile:RPGXML = New RPGXML
							'copy target files to export path
							If exportpath <> ""
								'CopyDir( "target/html5",exportpath )
								myfile.CreateXML(exportpath,mainwin)	
							EndIf	

												
						Case MENU_EXPORT_HTML5
							Local exportpath:String=RequestDir("Export HTML5 to Folder",CurrentDir())
							Local myfile:RPGXML = New RPGXML
							'copy target files to export path
							If exportpath <> ""
								CopyDir( "target/html5",exportpath )
								myfile.CreateXML(exportpath+"/data",mainwin)	
							EndIf	
							
						Case MENU_EXPORT_FLASH
							Local exportpath:String=RequestDir("Export Flash to Folder",CurrentDir())
							Local myfile:RPGXML = New RPGXML
							'copy target files to export path
							If exportpath <> ""
								CopyDir( "target/flash",exportpath )
								myfile.CreateXML(exportpath+"/data",mainwin)	
							EndIf										
				End Select
				
				
				
		End Select
	End Method

	Method Setup()
	
		mainwin = CreateWindow( AppTitle, 0, 0, ClientWidth(Desktop())/4, ClientHeight(Desktop())/4, Null, WINDOW_TITLEBAR|WINDOW_MENU|WINDOW_STATUS|WINDOW_CENTER )
		filemenu=CreateMenu("&File",0,WindowMenu(mainwin))
		CreateMenu("&New",MENU_NEW,filemenu,KEY_N,MODIFIER_COMMAND)
		CreateMenu("&Open",MENU_OPEN,filemenu,KEY_O,MODIFIER_COMMAND)
		CreateMenu("&Close",MENU_CLOSE,filemenu,KEY_W,MODIFIER_COMMAND)
		CreateMenu("",0,filemenu)
		CreateMenu("&Save",MENU_SAVE,filemenu,KEY_S,MODIFIER_COMMAND)
		CreateMenu("",0,filemenu)
		CreateMenu("&Import",MENU_IMPORT,filemenu,KEY_I,MODIFIER_COMMAND)
		Local exportmenu:TGadget=CreateMenu("&Export",0,filemenu)
		CreateMenu("RPGXML",MENU_EXPORT_XML,exportmenu,0,MODIFIER_COMMAND)
		CreateMenu("HTML5",MENU_EXPORT_HTML5,exportmenu,0,MODIFIER_COMMAND)		
		CreateMenu("Flash",MENU_EXPORT_FLASH,exportmenu,0,MODIFIER_COMMAND)	
		
		CreateMenu("",0,filemenu)
		CreateMenu("E&xit",MENU_EXIT,filemenu,KEY_F4,MODIFIER_COMMAND)
		Rem
		editmenu=CreateMenu("&Edit",0,WindowMenu(mainwin))
		Local gfxed:TGadget = CreateMenu("Graphics",MENU_GFX,editmenu,KEY_X,MODIFIER_COMMAND)
		CreateMenu("Map Tiles",MENU_TILESET,gfxed)
		CreateMenu("Walkabout",MENU_WALKABOUT,gfxed)
		CreateMenu("Hero",MENU_HERO,gfxed)
		CreateMenu("Small Enemy",MENU_SM_ENEMY,gfxed)
		CreateMenu("Medium Enemy",MENU_MD_ENEMY,gfxed)
		CreateMenu("Large Enemy",MENU_LG_ENEMY,gfxed)

		CreateMenu("Attacks",MENU_ATTACKS,gfxed)
		CreateMenu("Weapons",MENU_WEAPONS,gfxed)
		CreateMenu("Box Edges",MENU_BOXES,gfxed)
		CreateMenu("Portraits",MENU_PORTRAIT,gfxed)
		End Rem
		helpmenu=CreateMenu("&Help",0,WindowMenu(mainwin))
		CreateMenu("&About",MENU_ABOUT,helpmenu)
		
		UpdateWindowMenu mainwin
		
		'Super.Setup()
	
	End Method


End Type



Type SpriteSelector Extends EditorWindow
	
	Field sstate:SpriteState
	Field captions:String[]
	Field wintitle:String
	Field setspanel:TGadget
	Field spritew:Int,spriteh:Int
	Field zoom:Int = 1
	Field scroller:TGadget
	Method Setup(sspritetype:Int,frame_captions:String[],p:EditorWindow)
		
		parent = p
		captions = frame_captions

		'Soon i plan to move these functions to a complete importer and then convert it all, but for the time being they can stay here
	
		Select sspritetype
		
			Case SpriteType.sprTypeWalkabout
				'walkabout_frame_captions(7) as String = {"Up A","Up B","Right A","Right B","Down A","Down B","Left A","Left B"}
				sstate = WalkaboutState
				'sprite 20, 20, gen(genMaxNPCPic),    8, 5, walkabout_frame_captions(),  4, 4
				'sstate.CreateState(20,20,genMaxNPCPic,8,5,10,4)
				spriteh = 20
				spritew = 20
				wintitle = "Walkabout Sprites"
				zoom = 3
			
			Case SpriteType.sprTypeHero
				'walkabout_frame_captions(7) as String = {"Up A","Up B","Right A","Right B","Down A","Down B","Left A","Left B"}
				sstate = HeroState
				'sprite 32, 40, gen(genMaxHeroPic),   8, 16, hero_frame_captions(), 4, 0
				'sstate.CreateState(32,40,genMaxHeroPic,8,16,8,0)
				spriteh = 40
				spritew = 32
				wintitle = "Hero Sprites"
				zoom = 2

			Case SpriteType.sprTypeSmallEnemy
				'walkabout_frame_captions(7) as String = {"Up A","Up B","Right A","Right B","Down A","Down B","Left A","Left B"}
				sstate = SmallEnemyState
				'sprite 32, 40, gen(genMaxHeroPic),   8, 16, hero_frame_captions(), 4, 0
				'sstate.CreateState(34,34,genMaxEnemy1Pic,1,2,5,1)
				spriteh = 34
				spritew = 34
				wintitle = "Small Enemy Sprites"
				zoom = 5

			Case SpriteType.sprTypeMediumEnemy
				'walkabout_frame_captions(7) as String = {"Up A","Up B","Right A","Right B","Down A","Down B","Left A","Left B"}
				sstate = MediumEnemyState
				'sprite 50, 50, gen(genMaxEnemy2Pic), 1, 4, enemy_frame_captions(), 2, 2
				'sstate.CreateState(50,50,genMaxEnemy2Pic,1,4,4,2)
				spriteh = 50
				spritew = 50
				wintitle = "Medium Enemy Sprites"
				zoom = 4

			Case SpriteType.sprTypeLargeEnemy
				'walkabout_frame_captions(7) as String = {"Up A","Up B","Right A","Right B","Down A","Down B","Left A","Left B"}
				sstate = LargeEnemyState
				'sprite 80, 80, gen(genMaxEnemy3Pic), 1, 10, enemy_frame_captions(), 2, 3
				'sstate.CreateState(80,80,genMaxEnemy3Pic,1,10,3,3)
				spriteh = 80
				spritew = 80
				wintitle = "Large Enemy Sprites"
				zoom = 3
				
			Case SpriteType.sprTypeAttack
				'walkabout_frame_captions(7) as String = {"Up A","Up B","Right A","Right B","Down A","Down B","Left A","Left B"}
				sstate = AttackState
				'sprite 50, 50, gen(genMaxAttackPic), 3, 12, attack_frame_captions(), 2, 6
				'sstate.CreateState(50,50,genMaxAttackPic,3,12,4,6)
				spriteh = 50
				spritew = 50
				wintitle = "Attack Sprites"
				zoom = 4

			Case SpriteType.sprTypeWeapon
				'walkabout_frame_captions(7) as String = {"Up A","Up B","Right A","Right B","Down A","Down B","Left A","Left B"}
				sstate = WeaponState
				'sprite 24, 24, gen(genMaxWeaponPic), 2, 2, weapon_frame_captions(), 4, 5
				'sstate.CreateState(24,24,genMaxWeaponPic,2,2,6,5)
				spriteh = 24
				spritew = 24
				wintitle = "Weapon Sprites"
				zoom = 6

			Case SpriteType.sprTypeBoxBorder
				'walkabout_frame_captions(7) as String = {"Up A","Up B","Right A","Right B","Down A","Down B","Left A","Left B"}
				sstate = BoxState
				'sprite 16, 16, gen(genMaxBoxBorder), 16, 7, box_border_captions(), 4, 7
				'sstate.CreateState(16,16,genMaxBoxBorder,16,7,10,7)
				spriteh = 16
				spritew = 16
				wintitle = "Box Borders"
				zoom = 3

			Case SpriteType.sprTypePortrait
				'walkabout_frame_captions(7) as String = {"Up A","Up B","Right A","Right B","Down A","Down B","Left A","Left B"}
				sstate = PortraitState
				'sprite 50, 50, gen(genMaxPortrait), 1, 4, portrait_captions(), 2, 8
				'sstate.CreateState(50,50,genMaxPortrait,1,4,4,8)
				spriteh = 50
				spritew = 50
				wintitle = "Portraits"
				zoom = 4

				
		
		End Select
		mainwin = CreateWindow(wintitle, 0, 0, ClientWidth(Desktop())/2, ClientHeight(Desktop())/2, Null, WINDOW_TITLEBAR|WINDOW_TOOL|WINDOW_CENTER )
		
		setspanel = CreatePanel(0,0,ClientWidth(mainwin)-20,sstate.sets.Length*(spriteh+10),mainwin,PANEL_GROUP,captions[0])
		DebugLog "NUmsets Variable: "+sstate.numsets+" Real sets: "+sstate.sets.Length
		For Local i:Int = 0 To sstate.numsets-1
		
			For Local j:Int = 0 To sstate.sets[i].frames.Length-1
			
			
				Local spritepanel:TGadget = CreatePanel(j*((spritew*zoom)+10),i*((spriteh*zoom)+10),spritew*zoom,spriteh*zoom,setspanel,PANEL_ACTIVE)
				SetPanelPixmap( spritepanel,sstate.sets[i].frames[j].sprite,PANELPIXMAP_STRETCH)
				Local data:XYPair = New XYPair
				data.x = i
				data.y = j
				SetGadgetExtra(spritepanel,data)
				gadget_children.AddLast(spritepanel)
			
			Next			
		
		Next
		Local ssize:Int = sstate.sets.Length*(spriteh+10)
		scroller = CreateSlider(ClientWidth(mainwin)-20,0,20,ClientHeight(mainwin),mainwin,SLIDER_VERTICAL)
		SetSliderRange( scroller,0,ssize)
		SetSliderValue( scroller,0 )
	
	End Method
	
	
	
	Method UpdateUI(EID:Int)
		
		Select EID
			Case EVENT_WINDOWCLOSE
				
				Select EventSource()
				
					Case mainwin
						parent.children.Remove(Self)
						FreeGadget(mainwin)
						
						
				
				End Select
		End Select
		
		For Local p:TGadget = EachIn gadget_children
		
			Select EID 
			
				
				Case EVENT_MOUSEUP
					Select EventSource()
					
					
						Case p
							DebugLog "Panel Mouse Up"
							'go into sprite editor
							Local sped:SpriteEditor = New SpriteEditor
							Local data:XYPair = XYPair(GadgetExtra(p))
							sped.Setup(sstate,data.x,data.y,Self)
							children.Addlast(sped)
							
						
					End Select
				
				
				Case EVENT_GADGETACTION
					Select EventSource()
					
						Case scroller
							'DebugLog "Scroller moved: "+EventData()
							SetGadgetShape( setspanel,0,-EventData(),GadgetWidth(setspanel),GadgetHeight(setspanel) )

					
						Case p
							DebugLog "Panel Changed"
					
					End Select
					
			
			End Select
			
		Next
		For Local e:EditorWindow = EachIn children
			e.UpdateUI(EID)
		Next

	End Method


End Type

Type MaptilesSelector Extends EditorWindow

	Field setspanel:TGadget
	Field sets:TilsetState'TileSState
	Method Setup(p:EditorWindow)
		sets = TileSState
		mainwin = CreateWindow("Tilesets", 0, 0, ClientWidth(Desktop())/2, ClientHeight(Desktop())/2, Null, WINDOW_TITLEBAR|WINDOW_TOOL|WINDOW_CENTER )
		setspanel = CreatePanel(0,0,ClientWidth(mainwin)-20,(sets.numsets+1)*200,mainwin,PANEL_GROUP)
		For Local i:Int = 0 To sets.numsets-1
	
	
			Local spritepanel:TGadget = CreatePanel(0,i*200,320,200,setspanel,PANEL_ACTIVE)
			SetPanelPixmap( spritepanel,sets.Tileset[i].spr.sprite,PANELPIXMAP_STRETCH)
			Local data:XYPair = New XYPair
			data.x = i
			SetGadgetExtra(spritepanel,data)
			gadget_children.AddLast(spritepanel)
		
		
	
		Next
	
	
		
	
	End Method

End Type

Type SpriteEditor Extends EditorWindow
	
	Field sstate:SpriteState
	Field set:Int
	Field frame:Int
	Field palette:TGadget
	Field palettepix:TPixmap
	Field toolbar:TGadget


	Method Setup(s:SpriteState,setnum:Int,f:Int,p:EditorWindow)
		canvastimer = CreateTimer(30)
		sstate = s
		set = setnum
		frame = f
		parent = p
		mainwin = CreateWindow( "Sprite Editor", 0, 0, ClientWidth(Desktop())/2, ClientHeight(Desktop())/2, Null, WINDOW_TITLEBAR|WINDOW_TOOL|WINDOW_CENTER )		 
		DebugLog "Canvas size: "+(sstate.sets[set].frames[frame].sprite.width*sstate.zoom)+" x "+(sstate.sets[set].frames[frame].sprite.height*sstate.zoom)
		canvas = CreateCanvas( 0,30,sstate.sets[set].frames[frame].sprite.width*sstate.zoom,sstate.sets[set].frames[frame].sprite.height*sstate.zoom,mainwin )
		toolbar = CreateToolbar("resources/editicons.png",0,0,ClientWidth(mainwin),30,mainwin)
		SetToolbarTips(toolbar, ["Draw", "Fill", "Ellipse","Rectangle","Line"] )
		
		'AddGadgetItem toolbar, "", 0, GADGETITEM_NORMAL
		
		'make palette gadget
		palette = CreatePanel(ClientWidth(mainwin)-144,30,128,128,mainwin,PANEL_SUNKEN)
		palettepix = CreatePixmap(16,16,PF_RGBA8888)'
		Local xx:Int = 0
		Local yy:Int = 0
		For Local i:Int = 0 To 255
							
				WritePixel(palettepix,xx,yy,DefPalette[sstate.poffset[setnum]].col[i])
		
			xx = xx + 1
			If xx >= 16 Then
				yy = yy + 1
				xx = 0
			EndIf
		
			
		Next
		SetPanelPixmap(palette,palettepix,PANELPIXMAP_STRETCH)

	End Method
	
	Method UpdateUI(EID:Int)
		Select EID
		
			Case EVENT_WINDOWCLOSE
				
				Select EventSource()
				
					Case mainwin
						FreeGadget(mainwin)
						ListRemove(parent.children,Self)
						
						
				
				End Select
		
			Case EVENT_GADGETPAINT
				UpdateCanvas()
				
			Case EVENT_TIMERTICK
				Select EventSource()
				
					Case canvastimer
						UpdateCanvas()
							
				
				End Select

		
			
		End Select
	
	End Method
	
	Method UpdateCanvas()
		'If canvas = Null Then Return
		'DebugLog "Update Canvas"
		SetGraphics CanvasGraphics(canvas)
		Cls
		SetBlend(ALPHABLEND)
		For Local y:Int = 0 To sstate.sets[set].frames[frame].sprite.height-1
			For Local x:Int = 0 To sstate.sets[set].frames[frame].sprite.width-1
	
				Local col:Int = sstate.sets[set].frames[frame].sprite.ReadPixel(x,y)
				Local colors:Int[] = IntToRGBA( col )
				SetColor(colors[0],colors[1],colors[2])
				DrawRect(x*sstate.zoom,y*sstate.zoom,sstate.zoom,sstate.zoom )
			
			Next
		Next
		Flip
	End Method


End Type
